/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MOVEMENT_NAPI_H
#define MOVEMENT_NAPI_H

#include <map>

#include "napi/native_api.h"
#include "napi/native_node_api.h"

#include "movement_callback_stub.h"
#include "movement_data_utils.h"
#include "movement_event.h"

namespace OHOS {
namespace Msdp {
class MovementCallback : public MovementCallbackStub {
public:
    explicit MovementCallback() {};
    virtual ~MovementCallback() {};
    void OnMovementChanged(const MovementDataUtils::MovementData& movementData) override;
};

class MovementNapi : public MovementEvent {
public:
    explicit MovementNapi(napi_env env, napi_value thisVar);
    virtual ~MovementNapi();

    static napi_value Init(napi_env env, napi_value exports);
    static napi_value SubscribeMovement(napi_env env, napi_callback_info info);
    static napi_value UnSubscribeMovement(napi_env env, napi_callback_info info);
    static napi_value GetMovement(napi_env env, napi_callback_info info);
    static napi_value EnumMovementTypeConstructor(napi_env env, napi_callback_info info);
    static napi_value CreateEnumMovementType(napi_env env, napi_value exports);
    static napi_value EnumMovementValueConstructor(napi_env env, napi_callback_info info);
    static napi_value ResponseConstructor(napi_env env, napi_callback_info info);
    static napi_status AddProperty(napi_env env, napi_value object, const std::string name, int32_t enumValue);
    static napi_value CreateMovementValueObject(napi_env env);
    static napi_value CreateResponseClass(napi_env env, napi_value exports);
    static napi_value CreateInstanceForResponse(napi_env env, int32_t value);
    static int32_t ConvertTypeToInt(std::string type);
    static void RegisterCallback(const int32_t& eventType);
    static void InvokeCallBack(napi_env env, napi_value *args, int32_t value);
    void OnMovementChangedDone(const int32_t& type, const int32_t& value, bool isOnce);
    static MovementNapi* GetMovementNapi(int32_t type);
    static std::map<int32_t, sptr<ImovementCallback>> callbackMap_;
    static std::map<int32_t, MovementNapi*> objectMap_;

private:
    napi_ref callbackRef_;
    static napi_ref movementValueRef_;
    napi_env env_;
};
} // namespace Msdp
} // namespace OHOS
#endif // MOVEMENT_NAPI_H
